//
//  SearchSpaceViewController.m
//  Arts1
//
//  Created by 孤星晨宇 on 15/10/7.
//  Copyright © 2015年 Arts1. All rights reserved.
//

#import "SearchSpaceViewController.h"
#import "SpaceModel.h"
#import "AllHeader.h"

#define seaSURL @"http://arts1.dlab.com.cn/app/space"

@interface SearchSpaceViewController ()<UISearchBarDelegate,UITableViewDataSource, UITableViewDelegate>

// 搜索框tableView
@property (nonatomic, strong) UISearchBar *searchBar;

// 搜索结果展示
@property (nonatomic, strong) UITableView *resultView;

// 搜索结果数据
@property (nonatomic, strong) NSMutableArray *resultArr;



@end

@implementation SearchSpaceViewController

- (NSMutableArray *)resultArr{
    if (!_resultArr) {
        self.resultArr = [NSMutableArray array];
    }
    return _resultArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    [self createNavigationView];
}
// 高校和画室的地区请求
- (void)requestData
{
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    manage.responseSerializer.acceptableContentTypes  =[NSSet setWithObject:@"text/html"];
    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    _search = _searchBar.text;
    
    // 请求参数
    NSDictionary *paraDic =@{@"type":_type,@"search":_search};
    
    [manage POST:seaSURL parameters:paraDic
         success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         // 请求成功的时候调用这个block
         NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
         // status 的值为1时，操作成功，可以使用数据
         NSInteger status = [dic[@"status"] integerValue];
         if (status == 1) {
             NSArray *areaArr = dic[@"list"];
             for (NSDictionary *areadic in areaArr) {
               SpaceModel *space = [[SpaceModel alloc] init];
                 [SpaceModel setValuesForKeysWithDictionary:areadic];
                 [self.resultArr addObject:space];
             }
         }
         NSLog(@" 高校或画室搜索结果 ： %@ ",dic);
         
         
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         // 请求失败的时候调用调用这个block
         NSLog(@"请求失败");
     }];
    
}


- (void)createNavigationView{
    UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [UIView viewShadowColor:whiteView];
    [self.view addSubview:whiteView];
    
    // 搜索框
    _searchBar = [[UISearchBar alloc] init];
    [_searchBar setFrame:(CGRectMake(2, 25, SCREEN_WIDTH - 4, 34))];
    _searchBar.searchBarStyle = UISearchBarStyleDefault;
    _searchBar.placeholder = @"search";
    _searchBar.delegate = self;
    _searchBar.showsCancelButton = YES;
     [whiteView addSubview:_searchBar];
    
    // UISearchBar 有两个的子视图有两个姿势图，一个是背景视图，一个是textfield
    UIView *view = _searchBar.subviews[0];
    [view.subviews[0] removeFromSuperview];
    UITextField *textfield = (UITextField *)view.subviews[0];
    textfield.backgroundColor = kCustomColor(230, 230, 230, 1);
    
    for(id cc in [_searchBar subviews]){
        if([cc isKindOfClass:[UIButton class]]){
            UIButton *btn = (UIButton *)cc;
            [btn setTitle:@"C" forState:UIControlStateNormal];
        }
    }
}

// 搜索结果展示视图
- (void)createResultTableView{
    _resultView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:(UITableViewStylePlain)];
    _resultView.dataSource = self;
    _resultView.delegate = self;
    
}

#pragma mark ---- UISearchBarDelegate
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    /**
     *  点击搜索框的 搜索 按钮后执行的事件
     */
    [self requestData];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    /**
     *  点击搜索框的 取消 按钮后执行的事件
     */
    [_searchBar resignFirstResponder];
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark --- UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _resultArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"searchCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"searchCell"];
    }
    SpaceModel *model = (SpaceModel *)_resultArr[indexPath.row];
    cell.textLabel.text = model.spaceName;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SpaceModel *model = (SpaceModel *)_resultArr[indexPath.row];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
